<template>
  <div class="mod-config editViewContainer">
    <split-pane class="splitPane" :min-percent='10' :default-percent='10' split="vertical">
      <template slot="paneL">
        <div class="treeViewContainer">
          <tree-view ref="treeView" :treeData="statusTreeList" :checkChangedHandler="getDataList"></tree-view>
        </div>
      </template>
      <template slot="paneR">
        <table-view ref="view"
                    :searchAttrs="searchAttrs"
                    :primaryKey="primaryKey"
                    :listApiName="listApiName"
                    :showAttrs="showAttrs"
        >
          <template v-slot:foot="{scope}">
            <el-tooltip  class="item" effect="dark" content="更新质检状态" placement="bottom" :hide-after="1800" :open-delay="800">
              <el-button  type="text" size="small"  @click="openQualityApprovalDialog(R.prop('row', scope))"  >更新质检</el-button>
            </el-tooltip>
            <el-tooltip   class="item" effect="dark" content="查看质检记录" placement="bottom" :hide-after="1800" :open-delay="800">
              <el-button  type="text" size="small" @click="openQualityApprovalListDialog(R.prop('row', scope))" >查看记录</el-button>
            </el-tooltip>
<!--         @click="openBoxOrderStatusDialog(R.prop('row', scope))"-->
          </template>

        </table-view>

      </template>
    </split-pane>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <el-dialog title="查看质检记录" :visible.sync="qualityApprovalDialog" v-loading="qualityApprovalLoading" width="50%">

      <el-table :data="qualityApprovalList" border style="width: 100%;">
        <el-table-column prop="qualifiedNumber" header-align="center" align="center" label="采购数量"></el-table-column>
        <el-table-column prop="qualityFilePath" header-align="center" align="center" label="抽检报告">
          <template slot-scope="scope">
            <el-tag style="cursor:pointer;" @click="openOtherWindowHandle(scope.row.qualityFilePath)">下载文件</el-tag>
          </template>

        </el-table-column>
        <el-table-column prop="qualityRemark" header-align="center" align="center" label="质检备注">
        </el-table-column>
        <el-table-column prop="qualityStatus" header-align="center" align="center" label="质检状态">
          <template slot-scope="scope">
            {{qualityApprovalStatus[scope.row.qualityStatus]}}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" header-align="center" align="center" label="日期"></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
            <el-button @click="qualityApprovalDialog = false">取 消</el-button>
            <el-button type="primary" @click="qualityApprovalDialog = false">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>


<script>
  import tableView from '@/components/tableView'
  import basicMixins from '@/utils/commonViewMixins'
  import AddOrUpdate from './qualityapprovalinfo-add-or-update'
  import treeView from '@/components/treeView'
  import {mRequest} from '@/utils/request'
  import {API_DEFAULT_CONFIG} from '@/config'

  export default {
    mixins: [basicMixins],
    data() {
      return {
        statusTreeList: [],
        primaryKey: 'id',
        qualityApprovalList: [],
        qualityApprovalLoading: false,
        qualityApprovalDialog: false,
        qualityApprovalApiName: 'qualityApi/getQualityApprovalInfoList',
        listApiName: 'purchaseApi/listPurchasePartList',
        //    deleteApiName: 'purchaseApi/deletePurchasePart',
        exportApiName: 'purchaseApi/getPurchaseModel',
        saveAuthName: 'purchase:purchasepart:save',
        deleteAuthName: 'purchase:purchasepart:delete',
        searchAttrs: {
          contractPurchaseId: {
            type: 'autoComplete',
            placeholder: '请输入采购合同号',
            querySearchApiName: 'contractApi/getContractPurchaseInfoList'
          },
          sysDictId: {
            type: 'autoComplete',
            placeholder: '请输入配件编号',
            querySearchApiName: 'systemApi/getBottleAndCapList'
          },
          supplierId:{
            type: 'autoComplete',
            placeholder: '请输入供应商',
            querySearchApiName: 'supplierApi/getSupplierList'
          },
          status: {
            type: 'func',
            dataFunc: () => {
              return this.$refs.treeView.$refs.treeForm.getCheckedKeys()[0]
            }
          }
        },
        addOrUpdateVisible: false,
        showAttrs: {
          supplierName: {
            label: '供应商'
          },
          userName:{
            label: '申请人'
          },
          sysDictName: {
            label: '配件信息'
          },
          imagePath: {
            label: '图片', type: 'image'
          },
          purchaseNumber: {
            label: '采购数量'
          },
          createTime: {
            label: '采购日期',
            type:'date'
          },
          remark:{
            label:"采购备注"
          },
          qualityFilePath:{
            label: '抽检报告', showKey: 'sysDictName', type: 'file', width: 140
          },
          qualityRemark:{
            label:"质检备注"
          },
          qualityStatus:{
            label: "质检状态",
            type: 'radio',
            items: [
              {value: 17, text: '挑选', tagType: 'warning'},
              {value: 18, text: '合格', tagType: 'success'},
              {value: 19, text: '不合格', tagType: 'danger'},
              {value: 20, text: '特采', tagType: 'success'}
            ]

          },

          status: {
            label: '状态',
            type: 'radio',
            items: [
              {value: 0, text: '待采购', tagType: 'info'},
              {value: 1, text: '采购申请待审批'},
              {value: 2, text: '采购进行中'},
              {value: 3, text: '采购逾期预警', tagType: 'warning'},
              {value: 4, text: '采购完成', tagType: 'success'},
              {value: 7, text: '采购入库进行中', tagType: 'warning'},
              {value: 8, text: '采购入库完成', tagType: 'success'},
              {value: 17, text: '挑选', tagType: 'warning'},
              {value: 18, text: '合格', tagType: 'success'},
              {value: 19, text: '不合格', tagType: 'danger'},
              {value: 20, text: '特采', tagType: 'success'}
            ]
          }
        },
        qualityApprovalStatus: {
          17: '挑选',
          18: '合格',
          19: '不合格',
          20: '特采',
        },
        purchaseContractVisible: false,
        supplierList: [],
        sysDictList: [],
        orderDetailList: [],
        supplierId: '',
        contractTime: '',
        deliveryTime: '',
        totalAmount: 0,
        purchaseType: 14,
        remark: '',
        tableListData: [],
      }
    },
    components: {
      treeView,
      tableView,
      AddOrUpdate
    },
    methods: {
      exportPlantFunc(rows){

      },
      calculateTotalAmount() {
        this.totalAmount=0
        for (let i = 0; i <this.tableListData.length ; i++) {
          this.tableListData[i].totalPrice = this.tableListData[i].price * this.tableListData[i].purchaseNumber
          this.totalAmount += this.tableListData[i].totalPrice
        }
      },

      statusTreeListInit() {
        mRequest('systemApi/getPurchaseStatusList', {}, (res) => {
          this.statusTreeList = this.R.pathOr([], ['data', 'list'], res)
        })
      },
      initSupplierList() {
        mRequest('supplierApi/getSupplierList', {}, (res) => {
          this.supplierList = this.R.pathOr([], ['data', 'list'], res)
        })
      },
      initOrderDetailList() {
        mRequest('orderApi/getOrderDetailList', {}, (res) => {
          this.orderDetailList = this.R.pathOr([], ['data', 'list'], res)
        })
      },
      initSysDictList() {
        mRequest('systemApi/getBottleAndCapList', {}, (res) => {
          this.sysDictList = this.R.pathOr([], ['data', 'list'], res)
        })
      },
      openQualityApprovalDialog(cb) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          if (!this.R.isNil(this.$refs.addOrUpdate)) {
            this.$refs.addOrUpdate.init(null, {qualityType: 2,purchasePartId:cb.id}, cb)
          }
        })
      },
      openQualityApprovalListDialog(cb) {
        this.qualityApprovalList = []
        this.qualityApprovalDialog=true
        mRequest(this.qualityApprovalApiName, {'purchasePartId': cb.id}, (res) => {
          this.qualityApprovalList = this.R.pathOr([], ['data', 'list'], res)
        })

      },
      openOtherWindowHandle(url) {
        url = `${API_DEFAULT_CONFIG.baseURL}/pub/${url}?token=${this.$cookie.get('token')}`
        window.open(url)
      },
    },
    mounted() {
      this.statusTreeListInit()
    }
  }
</script>
